#include <inttypes.h>
#include <stm32l1xx.h>
#include <stdlib.h>
#include <stdio.h>

#include <init.h>
#include <vectors.h>
#include <usart.h>
#include <adc.h>
#include <term.h>
#include <flash.h>
#include <motor.h>
#include <irc.h>

#define STACK_TOP 0x20003800   // just a tiny stack for demo

//static void NMI_Handler(void);
//static void HardFault_Handler(void);
int main(void);
void NMI_Handler(void) {
    while(1) ;
}

void HardFault_Handler(void) {
	GPIOC -> ODR |= 0x8;

	while(1) ;
}


unsigned int *myvectors[67]
__attribute__ ((section("vectors"))) = {
	(unsigned int *) STACK_TOP,         // stack pointer
	(unsigned int *) main,              // code entry point
	(unsigned int *) NMI_Handler,       // NMI handler (not really)
	(unsigned int *) HardFault_Handler,  // hard fault handler
	(unsigned int *) MemManage_Handler,
	(unsigned int *) BusFault_Handler,
	(unsigned int *) UsageFault_Handler,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) SVC_Handler,
	(unsigned int *) DebugMon_Handler,
	(unsigned int *) 0,
	(unsigned int *) PendSV_Handler,
	(unsigned int *) SysTick_Handler,
	(unsigned int *) WWDG_IRQHandler,
	(unsigned int *) PVD_IRQHandler,
	(unsigned int *) TAMPER_STAMP_IRQHandler,
	(unsigned int *) RTC_WKUP_IRQHandler,
	(unsigned int *) FLASH_IRQHandler,
	(unsigned int *) RCC_IRQHandler,
	(unsigned int *) EXTI0_IRQHandler,
	(unsigned int *) EXTI1_IRQHandler,
	(unsigned int *) EXTI2_IRQHandler,
	(unsigned int *) EXTI3_IRQHandler,
	(unsigned int *) EXTI4_IRQHandler,
	(unsigned int *) DMA1_Channel1_IRQHandler,
	(unsigned int *) DMA1_Channel2_IRQHandler,
	(unsigned int *) DMA1_Channel3_IRQHandler,
	(unsigned int *) DMA1_Channel4_IRQHandler,
	(unsigned int *) DMA1_Channel5_IRQHandler,
	(unsigned int *) DMA1_Channel6_IRQHandler,
	(unsigned int *) DMA1_Channel7_IRQHandler,
	(unsigned int *) ADC1_IRQHandler,
	(unsigned int *) USB_HP_IRQHandler,
	(unsigned int *) USB_LP_IRQHandler,
	(unsigned int *) DAC_IRQHandler,
	(unsigned int *) COMP_IRQHandler,
	(unsigned int *) EXTI9_5_IRQHandler,
	(unsigned int *) LCD_IRQHandler,
	(unsigned int *) TIM9_IRQHandler,
	(unsigned int *) TIM10_IRQHandler,
	(unsigned int *) TIM11_IRQHandler,
	(unsigned int *) TIM2_IRQHandler,
	(unsigned int *) TIM3_IRQHandler,
	(unsigned int *) TIM4_IRQHandler,
	(unsigned int *) I2C1_EV_IRQHandler,
	(unsigned int *) I2C1_ER_IRQHandler,
	(unsigned int *) I2C2_EV_IRQHandler,
	(unsigned int *) I2C2_ER_IRQHandler,
	(unsigned int *) SPI1_IRQHandler,
	(unsigned int *) SPI2_IRQHandler,
	(unsigned int *) USART1_IRQHandler,
	(unsigned int *) USART2_IRQHandler,
	(unsigned int *) USART3_IRQHandler,
	(unsigned int *) EXTI15_10_IRQHandler,
	(unsigned int *) RTC_Alarm_IRQHandler,
	(unsigned int *) USB_FS_WKUP_IRQHandler,
	(unsigned int *) TIM6_IRQHandler,
	(unsigned int *) TIM7_IRQHandler,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) 0,
	(unsigned int *) BootRAM          /* @0x108. This is for boot in RAM mode for 
                            STM32L15x ULtra Low Power Medium-density devices. */ 
};

char bufa[8] __FLASH__ = {'0','1','2','3','4','5','6','7'};
char bufb[8] __FLASH__ = {'7','6','5','4','3','2','1','0'};
char gotopos[8] __FLASH__ = {0x1B,'[','1','0',';','1','0','H'}; // ESC [10;10H
char buf[9] __FLASH__ = "bufferxy";

int main(void)
{
//	int i;
	int omega,prud;

	init_IO();
	irc_init();
	motor_init();
	adc_init();
	usart_init();
	term_init();
/*
	while(1);
}
*/


	while(1) {
		static uint32_t ibuf;
		static uint8_t flagprev;

		if (flag != flagprev) {
			omega += omega_act;
			prud  += i_act;
			flagprev = flag;
		}

		if (flag > 99) {
			flag = 0;

			gotoPOS(1,2);
			USART1_DMAsend("+-------+---------------+---------------------------+",53);
		//                      12345678901234567890123456789012345678901234567890
			gotoPOS(1,3);
			USART1_DMAsend("|Reg.r. |Ziadana hodnota|     Skutocna  hodnota     |",53);
			gotoPOS(1,4);
			USART1_DMAsend("+-------+---------------+---------------------------+",53);
			gotoPOS(1,5);
			USART1_DMAsend("|Prud   |               |                           |",53);
			gotoPOS(1,6);
			USART1_DMAsend("+-------+---------------+---------------------------+",53);
			gotoPOS(1,7);
			USART1_DMAsend("|       |               |                           |",53);
			gotoPOS(1,8);
			USART1_DMAsend("+Otacky |Edit:          +---------------------------+",53);
			gotoPOS(1,9);
			USART1_DMAsend("|       |               |                           |",53);
			gotoPOS(26,9);
			if (ramp_on)
				USART1_DMAsend("Rampa zapnuta ",14);
			else
				USART1_DMAsend("Rampa vypnuta ",14);
			USART1_DMAsend(rampnum == 1 ? " 6" : rampnum == 2 ? "12" : rampnum == 5 ? "30" : " E",2);
			USART1_DMAsend(" min^-1s^-1",11);
			gotoPOS(1,10);
			USART1_DMAsend("+-------+---------------+---------------------------+",53);
			
			gotoPOS(11,5);
			USART1_sendINT(i_des*5357/16384,6); // 3V/0R56/32768*2, Uref/Ri/maxnum*2 (ocakavane zosilnenie bolo 2)
			USART1_DMAsend(" mA",3);
			gotoPOS(32,5);
			USART1_sendINT((prud/100)*5357/16384,7);
			USART1_DMAsend(" mA",3);
			
			gotoPOS(11,7);
			USART1_sendINT((omega_des*60)/360,6);
			USART1_DMAsend(" min^-1",7);
			gotoPOS(32,7);
			USART1_sendINT((((omega/100)*60)/360),7);
			USART1_DMAsend(" min^-1",7);
			gotoPOS(1,2);
			term_rewrite();
			gotoPOS(1,2);

			omega = 0;
			prud  = 0;

			ibuf = i_act;
		}

		gotoPOS(1,2);
		term_thread();
		gotoPOS(1,2);
	}
}

